package com.ccrfid.rmc6.resource.personnel;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.pojo.dto.sync.PrisonerCardDto;
import com.ccrfid.rmc6.pojo.dto.sync.SyncExternalDto;
import com.ccrfid.rmc6.pojo.dto.sync.SyncPrisonerDto;
import com.ccrfid.rmc6.pojo.dto.sync.SyncPoliceDto;
import com.ccrfid.rmc6.service.personnel.SyncPrisonerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.List;

@Path("sync")
@Produces(MediaType.APPLICATION_JSON)
public class SyncPrisonerResource {
    private static final Logger logger = LoggerFactory.getLogger(PersonnelResource.class);
    @Autowired
    private SyncPrisonerService syncPrisonerService;

    @GET
    @Path("updatetime")
    public Timestamp getUpdateTime() {
        return syncPrisonerService.getUpdateTime();
    }

    @GET
    @Path("prisoners/{personnelSn}")
    public SyncPrisonerDto getPrisonerInfo(@PathParam("personnelSn") String personnelSn) {
        return syncPrisonerService.getPrisonerInfo(personnelSn);
    }

    @PUT
    @Path("{prisonId}/prisoners")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean updatePrisonerInfo(@PathParam("prisonId") String prisonId,
                                      SyncPrisonerDto prisonerDto) throws AppException {
        syncPrisonerService.updatePrisonerInfo(prisonId, prisonerDto);
        return true;
    }

    @GET
    @Path("{personnelSn}/photo")
    @Produces("image/jpeg")
    public byte[] getPrisonerPhoto(@PathParam("personnelSn") String personnelSn) {
        return syncPrisonerService.getPrisonerPhoto(personnelSn);
    }

    @PUT
    @Path("{personnelSn}/photo")
    @Consumes(MediaType.APPLICATION_OCTET_STREAM)
    public Boolean updatePrisonerPhoto(@PathParam("personnelSn") String personnelSn, byte[] image)
            throws AppException {
        syncPrisonerService.updatePrisonerPhoto(personnelSn, image);
        return true;
    }

    @GET
    @Path("{personnelSn}/photo-existence")
    public Boolean existPhoto(@PathParam("personnelSn") String personnelSn) throws AppException {
        return syncPrisonerService.existPhoto(personnelSn);
    }

    @GET
    @Path("all-prisoners-with-card")
    public List<PrisonerCardDto> getAllPrisonersWithCard() {
        return syncPrisonerService.getAllPrisonersWithCard();
    }

    //获取某个警员信息
    @GET
    @Path("personnel/{personnelSn}")
    public SyncPoliceDto getPoliceInfo(@PathParam("personnelSn") String personnelSn) throws AppException {
        return syncPrisonerService.getPoliceInfo(personnelSn);
    }

    //更新某个警员信息
    @PUT
    @Path("{prisonId}/personnel")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean updatePoliceInfo(@PathParam("prisonId") String prisonId,
                                    SyncPoliceDto policeDto) throws AppException {
        syncPrisonerService.updatePoliceInfo(prisonId, policeDto);
        return true;
    }

    //获取某个外来人员信息
    @GET
    @Path("ext-personnel/{personnelSn}")
    public SyncExternalDto getExternalPerson(@PathParam("personnelSn") String personnelSn) throws AppException {
        return syncPrisonerService.getExternalPersonInfo(personnelSn);
    }

    //更新某个外来人员信息
    @PUT
    @Path("{prisonId}/ext-personnel")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean updateExternalPerson(@PathParam("prisonId") String prisonId,
                                        SyncExternalDto externalDto) throws AppException {
        syncPrisonerService.updateExternalPersonInfo(prisonId, externalDto);
        return true;
    }
}
